//
//  RnD_02_rolling_spheres_vanilla_SIO2AppDelegate.mm
//  RnD_02_rolling_spheres_vanilla_SIO2
//
//  Created by Goffredo Marocchi on 7/22/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

//File modified by Goffredo Marocchi (this is not the original SIO2 provided file,
//sio2 and its creator ROm are not responsible for the content of this file as it is now

#import "SysTools.h"

#import "RnD_02_rolling_spheres_vanilla_SIO2AppDelegate.h"

@implementation RnD_02_rolling_spheres_vanilla_SIO2AppDelegate

@synthesize window;
@synthesize glView;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
	NSLog(@"Application Did finish Launching");
	[SysTools testRightShift];
	//[SysTools testIsNumber]; //numbers parsing test
	
	// Flip the simulator to the right
	[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight
													  animated: NO];
	// Start the accelerometer with a rate of 60 fps
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:( 1.0f / 60.0f )];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
	
	[SysTools sendOrientationNotifications:@selector(didRotateMethod:) to:self];
	
	glView.animationInterval = 1.0f / 60.0f;
	[glView startAnimation];
	
	[ [UIApplication sharedApplication] setIdleTimerDisabled:NO ];	
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	NSLog(@"Application Did Become Active");
	[glView startAnimation];
}

- (void)applicationWillResignActive:(UIApplication *)application {
	NSLog(@"Application Will Resign Active");
	[glView stopAnimation];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     DidEnterBackground --> WillEnterForeground --> DidBecomeActive
	 */
	NSLog(@"Application has entered foreground");
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
	NSLog(@"Application has entered background");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate and multi-tasking is not supported.
     See also applicationDidEnterBackground:.
     */
	NSLog(@"Application Will Terminate... iOS < 4.0 or backgrounding disabled");
	[glView stopAnimation];
}

- (void)didRotateMethod:(NSNotification*) sender {
	
	UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
	
	switch (deviceOrientation) {
		case UIDeviceOrientationUnknown:
			break;
		case UIDeviceOrientationLandscapeLeft:
			NSLog(@"Landscape left");
			break;
		case UIDeviceOrientationLandscapeRight:
			NSLog(@"Landscape right");
			break;
		case UIDeviceOrientationPortrait:
			NSLog(@"Portrait up");
			break;
		case UIDeviceOrientationPortraitUpsideDown:
			NSLog(@"Portrait down");
			break;
		case UIDeviceOrientationFaceUp:
			NSLog(@"Face up");
			break;
		case UIDeviceOrientationFaceDown:
			NSLog(@"Face Down");
			break;
		default:
			NSLog(@"deviceOrientation = %i", deviceOrientation);
			NSAssert(FALSE, @"Wrong notification delivered... not a valid UIDeviceOrientation notification");
			break;
	}
}

- (void)dealloc {
	[window release];
	[glView release];
	[super dealloc];
}


// Create an handler that is going to help us dispatch
// the accelerometer events to our main window event
// structure. Remember to extend your main class to support
// the acceleromter ( templateAppDelegate.h line #16 )
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
	// Uncomment this block for having a smooth accelerometer
	// interpolation, instead well... its a bit jumpy :)
	/*
	 sio2->_SIO2window->accel->x = acceleration.x * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->x * sio2->_SIO2window->accel_smooth;
	 sio2->_SIO2window->accel->y = acceleration.y * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->y * sio2->_SIO2window->accel_smooth;
	 sio2->_SIO2window->accel->z = acceleration.z * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->z * sio2->_SIO2window->accel_smooth;	
	 
	 sio2->_SIO2window->accel->x = ( int )( sio2->_SIO2window->accel->x * 100.0f );
	 sio2->_SIO2window->accel->x *= 0.01f;
	 
	 sio2->_SIO2window->accel->y = ( int )( sio2->_SIO2window->accel->y * 100.0f );
	 sio2->_SIO2window->accel->y *= 0.01f;		
	 */
	
	sio2->_SIO2window->accel->x = acceleration.x;
	sio2->_SIO2window->accel->y = acceleration.y;
	sio2->_SIO2window->accel->z = acceleration.z;	
	
	sio2ResourceDispatchEvents( sio2->_SIO2resource,
							   sio2->_SIO2window,
							   SIO2_WINDOW_ACCELEROMETER,
							   SIO2_WINDOW_TAP_NONE );
}


@end
